﻿using DynamicPatcher;
using Extension.MyExtension.InternalComponents;
using Extension.Utilities;
using PatcherYRpp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Extension.Ext
{
    public partial class TechnoExt
    {
        private bool virtualTurretActived = false;

        public unsafe void TechnoClass_Init_Virtual_Turret()
        {

        }

        public unsafe void TechnoClass_Update_Virtual_Turret()
        {
            if (virtualTurretActived == false && OwnerObject.Ref.Base.IsOnMap)
            {
                virtualTurretActived = true;


                if (string.IsNullOrEmpty(Type.VirtualTurretTypes))
                    return;
                if (string.IsNullOrEmpty(Type.VirtualTurretLocations))
                    return;
                if (OwnerObject.Ref.Owner.IsNull)
                    return;

                List<string> types = new List<string>();

                List<CoordStruct> locations = new List<CoordStruct>();

                types = Type.VirtualTurretTypes.Split(',').ToList();
                locations = Type.VirtualTurretLocations.Replace("(", "").Split(')').Where(t => !string.IsNullOrEmpty(t)).Select(c =>
                {
                    var xyz = c.Split(',').ToList().Select(x =>
                    {
                        return int.Parse(x);
                    }
                    ).ToList();
                    return new CoordStruct(xyz[0], xyz[1], xyz[2]);
                }
                ).ToList();

                for (var i = 0; i < types.Count(); i++)
                {
                    CoordStruct flh = locations.Count() > i ? locations[i] : new CoordStruct(0, 0, 0);
                    var turType = types[0];

                    Pointer<TechnoTypeClass> pType = TechnoTypeClass.ABSTRACTTYPE_ARRAY.Find(turType);
                    var techno = pType.Ref.Base.CreateObject(OwnerObject.Ref.Owner).Convert<TechnoClass>();

                    var ext = new ExtensionReference<TechnoExt>();
                    ext.Set(techno);

                    Logger.Log(techno.Ref.Base.Put(OwnerObject.Ref.Base.Base.GetCoords(), Direction.N));


                    if (ext.TryGet(out TechnoExt pTargetExt))
                    {
                        if (pTargetExt.Get(VirtualTurretComponent.ID) == null)
                        {
                            pTargetExt.CreateDecorator<VirtualTurretComponent>(VirtualTurretComponent.ID, "VirtualTurretComponent", ext.Get(), this, flh);
                        }
                    }


                }


                types.Clear();
                locations.Clear();
            }

        }

    }

    public partial class TechnoTypeExt
    {

        private bool virtualTurretInited = false;

        public string VirtualTurretTypes;

        public string VirtualTurretLocations;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="section"></param>
        private void ReadVirtualTurretData(INIReader reader, string section)
        {
            if (!virtualTurretInited)
            {
                virtualTurretInited = true;
                reader.ReadNormal(section, "VirtualTurret.Types", ref VirtualTurretTypes);
                reader.ReadNormal(section, "VirtualTurret.Locations", ref VirtualTurretLocations);
            }


        }
    }
}
